//
//  HZRewardView.m
//  Stock_Stategy
//
//  Created by zeroing on 17/6/2.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZRewardView.h"
#import "HZPopMenuView.h"
#import "HZRewardTableViewCell.h"

#define kTopBgViewH 45

@interface HZRewardView()<HZPopMenuViewDelegate,UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)HZPopMenuView    *pPopMenuView;
@property (nonatomic, strong)UITableView      *pTableView;
@property (nonatomic, strong)NSArray          *pDataList;
@end

@implementation HZRewardView
#pragma mark -
#pragma mark 生命周期
- (instancetype)init{
    self = [super init];
    if (self) {
        [self commonInit];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
#pragma mark -
#pragma mark private
- (void)commonInit{
    _pPopMenuView = [HZPopMenuView new];
    _pPopMenuView.delegate = self;
    
    _pTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _pTableView.delegate = self;
    _pTableView.dataSource = self;
    _pTableView.tableFooterView = [UIView new];
    _pTableView.backgroundColor = [UIColor clearColor];
    
    [self addSubview:_pPopMenuView];
    [self addSubview:_pTableView];
    
    [self p_initConstraints];
    
    NSArray *pTitleArray = @[@"盈/亏",@"时间"];
    _pPopMenuView.titleArray = pTitleArray;
    [self p_setPopMenuData];
    
#warning test
    NSMutableArray *modelsArray = [NSMutableArray arrayWithCapacity:10];
    HZRewardListModel *model1 = [HZRewardListModel new];
    model1.model_name = @"老板电器";
    model1.model_status = @"执行盈亏";
    model1.model_buyPrice = @"130.00";
    model1.model_num = @"200股";
    model1.model_buyMoney = @"1万元";
    model1.model_curPrice = @"104.00";
    model1.model_perform = @"分配";
    [modelsArray addObject:model1];
    _pDataList = modelsArray;
}

- (void)p_initConstraints
{
    [_pPopMenuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.height.mas_equalTo(kTopBgViewH);
    }];
    [_pTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self);
        make.top.mas_equalTo(_pPopMenuView.mas_bottom);
    }];
}

- (void)p_setPopMenuData
{
//    _hasLoadingPopMenuData = YES;
    //初始化控件内容
    NSMutableArray *pContentArray = [NSMutableArray arrayWithCapacity:2];
    NSMutableArray *pTempNum = [NSMutableArray array];
    NSMutableArray *pTempDate = [NSMutableArray array];
    NSMutableArray *pTempBS = [NSMutableArray array];
    NSMutableArray *pTempStatus = [NSMutableArray array];
    NSArray *dateArray = [NSArray arrayWithObjects:
                          @"全部",
                          @"7天之内",
                          @"15天之内",
                          @"15天之外",
                          nil];
    NSArray *buysellArray = [NSArray arrayWithObjects:
                             @"全部",
                             @"盈利",
                             @"亏损",
                             nil];
    for (NSString *title in dateArray) {
        HZPopMenuModel *model = [HZPopMenuModel new];
        model.model_name = title;
        [pTempDate addObject:model];
        if ([title isEqualToString:@"全部"]) {
            model.isShowImage = YES;
        }
    }
    for (NSString *title in buysellArray) {
        HZPopMenuModel *model = [HZPopMenuModel new];
        model.model_name = title;
        [pTempBS addObject:model];
        if ([title isEqualToString:@"全部"]) {
            model.isShowImage = YES;
        }
    }
    
    [pContentArray addObject:pTempBS];
    [pContentArray addObject:pTempDate];
    _pPopMenuView.contentArray = pContentArray;
}

- (void)updateNewData:(NSArray *)data{

}

#pragma mark -
- (void)upToParentWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
    switch (type) {
        case kHZ_radioGroupClick_Action:
        {
//            [self.parent upToParentWithObjc:objc withType:type withParamter:paramter];
        }
            break;
        default:
            break;
    }
}
- (void)downToChildWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
}

#pragma mark -
#pragma mark HZPopMenuViewDelegate
- (void)popMenuViewDidSelectedRowWithContent:(NSArray *)content{
    
    NSMutableArray *pTempArray = [NSMutableArray arrayWithCapacity:5];
    NSString *pStatus,*pBS,*pNum,*pTime,*pStart, *pEnd;
    pBS = @"";
    pStatus = @"";
    pNum = @"";
//    if(_nQueryResultType == kQueryResult_buyResult_Type
//       || _nQueryResultType == kQueryResult_buyApply_Type) {
//        pNum = content[0];//发售编号
//        pTime = content[1];//时间
//    }else if(_nQueryResultType == kQueryResult_Delegat_Type){
//        pBS = content[0];
//        pStatus = content[1];
//        pNum = content[2];//发售编号
//        pTime = content[3];//时间
//    }else if(_nQueryResultType == kQueryResult_Deal_Type){
//        pBS = content[0];
//        pNum = content[1];//发售编号
//        pTime = content[2];//时间
//    }
    
    //买卖标志:1买入;2卖出
    if ([pBS isEqualToString:@"全部"] || [pBS isEqualToString:@"买卖单"]) {
        pBS = @"";
    }else if([pBS isEqualToString:@"买单"]){
        pBS = @"1";
    }else if([pBS isEqualToString:@"卖单"]){
        pBS = @"2";
    }
    
    //委托状态:0委托中;1部分成交;2部成部撤;3全部成交;4已撤
    if([pStatus isEqualToString:@"委托状态"] || [pStatus isEqualToString:@"全部"]){
        pStatus = @"";
    }else if([pStatus isEqualToString:@"委托中"]){
        pStatus = @"0";
    }else if([pStatus isEqualToString:@"部分成交"]){
        pStatus = @"1";
    }else if([pStatus isEqualToString:@"部成部撤"]){
        pStatus = @"2";
    }else if([pStatus isEqualToString:@"全部成交"]){
        pStatus = @"3";
    }else if([pStatus isEqualToString:@"已撤"]){
        pStatus = @"4";
    }
    
    pStart = @"";
    pEnd = @"";
    if ([pNum isEqualToString:@"全部"] || [pNum isEqualToString:@"发售编号"]) {
        pNum = @"";
    }
    if ([pTime isEqualToString:@"发起时间"]) {
        [MYCommonTool commonToolYMDTimeWithStart:&pStart end:&pEnd endDay:0 rangeDay:90];
    }else if ([pTime isEqualToString:@"7天之内"]) {
        [MYCommonTool commonToolYMDTimeWithStart:&pStart end:&pEnd endDay:0 rangeDay:7];
    }else if ([pTime isEqualToString:@"15天之内"]) {
        [MYCommonTool commonToolYMDTimeWithStart:&pStart end:&pEnd endDay:0 rangeDay:15];
    }else if ([pTime isEqualToString:@"15天之外"]) {
        [MYCommonTool commonToolYMDTimeWithStart:&pStart end:&pEnd endDay:15 rangeDay:90];
    }
    
//    if(_nQueryResultType == kQueryResult_buyResult_Type
//       || _nQueryResultType == kQueryResult_buyApply_Type) {
//        [pTempArray addObject:pNum];
//        [pTempArray addObject:pStart];
//        [pTempArray addObject:pEnd];
//    }else if(_nQueryResultType == kQueryResult_Delegat_Type){
//        [pTempArray addObject:pBS];
//        [pTempArray addObject:pStatus];
//        [pTempArray addObject:pNum];
//        [pTempArray addObject:pStart];
//        [pTempArray addObject:pEnd];
//    }else if(_nQueryResultType == kQueryResult_Deal_Type){
//        [pTempArray addObject:pBS];
//        [pTempArray addObject:pNum];
//        [pTempArray addObject:pStart];
//        [pTempArray addObject:pEnd];
//    }
    
//    _actionClickPop(nil, pTempArray, nil);
}
#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _pDataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HZRewardListModel *model = _pDataList[indexPath.row];
    HZRewardTableViewCell *cell = [HZRewardTableViewCell cellTableView:tableView indexPath:indexPath cellModel:model];
    return cell;
}
#pragma mark -
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HZRewardListModel *model = _pDataList[indexPath.row];
    [self.parent upToParentWithObjc:self withType:kHZ_RewardListClick_Action withParamter:model];
}
@end
